#ifndef GENERARRECIBO_H
#define GENERARRECIBO_H

#include <QWidget>
#include <QDate>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlRelationalTableModel>
#include <QHeaderView>
#include <QMessageBox>
#include <QDebug>
#include <QItemSelectionModel>
#include <QItemSelection>
#include <QModelIndexList>
#include <QPrinter>
#include <QTextDocument>
#include <QTextCursor>
#include <QTextImageFormat>
#include <QFileDialog>
#include <QDir>
#include <QTextLength>
#include <QSqlRecord>
#include <QSqlError>
#include <QKeyEvent>
#include <QKeySequence>
#include <QShortcut>
#include <QDesktopServices>

namespace Ui {
class GenerarRecibo;
}

class GenerarRecibo : public QWidget
{
    Q_OBJECT

public:
    explicit GenerarRecibo(QWidget *parent = 0);
    GenerarRecibo(QWidget *parent, QString raz_soc);
    ~GenerarRecibo();

    void MandarMail();

private slots:
    void CargarClientes();

    void CambiarMes();
    void CambiarAnio();    

    void on_guardarBtn_clicked();
    void on_select_dir_btn_2_clicked();

    void LimpiarUI();

    void RefreshCCView();

private:
    Ui::GenerarRecibo *ui;

    QString razon_social;
    QString path;

    bool directorioPersonalizado;

};

class CCReciboSqlTableModel : public QSqlRelationalTableModel
{
    Q_OBJECT
public:
    CCReciboSqlTableModel(QObject * parent = 0, QSqlDatabase db = QSqlDatabase())
        : QSqlRelationalTableModel(parent,db) {;}
    QVariant data (const QModelIndex & index, int role = Qt::DisplayRole) const
    {
        if(role == Qt::BackgroundRole)
        {
            QModelIndex index2 = index.model()->index(index.row(), 10, index.parent()); // get index of "watched" column
            if (index.column() != 10) {
                if (index2.data(Qt::DisplayRole).toString() == "SI")
                    return QVariant(QColor(Qt::darkGreen));
            }
            else {
                const QVariant value(data(index,Qt::DisplayRole));
                if (value.toString() == "SI")
                    return QVariant(QColor(Qt::darkGreen));
            }
        }

        if(role == Qt::ForegroundRole)
        {
            QModelIndex index2 = index.model()->index(index.row(), 10, index.parent()); // get index of "watched" column
            if (index.column() != 10) {
                if (index2.data(Qt::DisplayRole).toString() == "SI")
                    return QVariant(QColor(Qt::white));
            }
            else {
                const QVariant value(data(index,Qt::DisplayRole));
                if (value.toString() == "SI")
                    return QVariant(QColor(Qt::white));
            }
        }

        if (role == Qt::TextAlignmentRole) {
            int col = index.column();
            if (col == 2 || col == 5 || col == 7 || col == 9 || col == 10) {
                return QVariant(Qt::AlignVCenter | Qt::AlignHCenter);
            }
            if (col == 11) {
                return QVariant(Qt::AlignVCenter | Qt::AlignRight);
            }
        }

        if (role == Qt::FontRole) {
            int col = index.column();
            if (col == 11)
                return QVariant (QFont("Helvetica57-Condensed",13,13));
            else
                return QVariant (QFont("Helvetica57-Condensed"));
        }

        return QSqlRelationalTableModel::data(index,role);
    }
};

#endif // GENERARRECIBO_H
